clear all
clf
hold off
set(gcf,'color','w')
colormap(parula(100))
axis off
format long
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make matlab maps: COUNTERFACTUAL 5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

addpath('...\replication\Section 6\01_data');
addpath('...\replication\altmany-export_fig-d570645');

com = load('...\replication\Section 6\01_data\com_final.csv');

L_ni = dlmread('...\replication\Section 6\01_data\commutes_ij_final.csv'); 
n_vars = dlmread('...\replication\Section 6\01_data\i_vars.csv'); 
L_i = sum(L_ni);
L_i_p = (L_i~=0);
L_ni = L_ni(L_i_p',L_i_p);
n_vars = n_vars(L_i_p',:);
dep = floor(n_vars(:,8));

Y_n = load('...\replication\Section 6\01_data\res_Y_n.csv');
Y_i = load('...\replication\Section 6\01_data\res_Y_i.csv');
L_i = load('...\replication\Section 6\01_data\res_L_i.csv');
R_n = load('...\replication\Section 6\01_data\res_R_n.csv');
w_i = load('...\replication\Section 6\01_data\res_w_i.csv');
v_n = load('...\replication\Section 6\01_data\res_v_n.csv');
Q_n = load('...\replication\Section 6\01_data\res_Q_n.csv');
com_n = load('...\replication\Section 6\01_data\res_com_n.csv');
Tcom_n = load('...\replication\Section 6\01_data\res_Tcom_n.csv');

gr_Y_i = 100.*((Y_i(:,4) - Y_i(:,1))./(Y_i(:,1)));
gr_Y_n = 100.*((Y_n(:,4) - Y_n(:,1))./(Y_n(:,1)));

gr_w_i = 100.*((w_i(:,4) - w_i(:,1))./(w_i(:,1)));
gr_v_n = 100.*((v_n(:,4) - v_n(:,1))./(v_n(:,1)));

gr_L_i = 100.*((L_i(:,4) - L_i(:,1))./(L_i(:,1)));
gr_R_n = 100.*((R_n(:,4) - R_n(:,1))./(R_n(:,1)));

gr_com_n = 100.*((com_n(:,4) - com_n(:,1))./(com_n(:,1)));
gr_Tcom_n = 100.*((Tcom_n(:,4) - Tcom_n(:,1))./(Tcom_n(:,1)));
gr_Q_n = 100.*((Q_n(:,4) - Q_n(:,1))./(Q_n(:,1)));

%% Collapse by departement

grY_n=accumarray(dep,gr_Y_n,[],@mean);
grY_i=accumarray(dep,gr_Y_i,[],@mean);
grL_i=accumarray(dep,gr_L_i,[],@mean);
grR_n=accumarray(dep,gr_R_n,[],@mean);
grw_i=accumarray(dep,gr_w_i,[],@mean);
grv_n=accumarray(dep,gr_v_n,[],@mean);
grQ_n=accumarray(dep,gr_Q_n,[],@mean);
grcom_n=accumarray(dep,gr_com_n,[],@mean);
grTcom_n=accumarray(dep,gr_Tcom_n,[],@mean);

grY_n = [grY_n(1:19);grY_n(21:end)];
grY_i = [grY_i(1:19);grY_i(21:end)];
grL_i = [grL_i(1:19);grL_i(21:end)];
grR_n = [grR_n(1:19);grR_n(21:end)];
grw_i = [grw_i(1:19);grw_i(21:end)];
grv_n = [grv_n(1:19);grv_n(21:end)];
grQ_n = [grQ_n(1:19);grQ_n(21:end)];
grcom_n = [grcom_n(1:19);grcom_n(21:end)];
grTcom_n = [grTcom_n(1:19);grTcom_n(21:end)];


%% Mape maps
france = shaperead('...\replication\Section 6\01_data\shapefile\dep_poly_metropole.shp');

%% Y_n
for ii = 1:numel(france)
  france(ii).pop = grY_n(ii,1); % add your data here
end

caxis([min(grY_n(:,1)) max(grY_n(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.2f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grY_n(:,1)) max(grY_n(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grY_n.pdf'


%% grY_i
for ii = 1:numel(france)
  france(ii).pop = grY_i(ii,1); % add your data here
end

caxis([min(grY_i(:,1)) max(grY_i(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.2f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grY_i(:,1)) max(grY_i(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grY_i.pdf'

%% grL_i
for ii = 1:numel(france)
  france(ii).pop = grL_i(ii,1); % add your data here
end

caxis([min(grL_i(:,1)) max(grL_i(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.2f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grL_i(:,1)) max(grL_i(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grL_i.pdf'

%% grR_n
for ii = 1:numel(france)
  france(ii).pop = grR_n(ii,1); % add your data here
end

caxis([min(grR_n(:,1)) max(grR_n(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.2f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grR_n(:,1)) max(grR_n(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grR_n.pdf'

%% grw_i
for ii = 1:numel(france)
  france(ii).pop = grw_i(ii,1); % add your data here
end

caxis([min(grw_i(:,1)) max(grw_i(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grw_i(:,1)) max(grw_i(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grw_i.pdf'

%% grv_n
for ii = 1:numel(france)
  france(ii).pop = grv_n(ii,1); % add your data here
end

caxis([min(grv_n(:,1)) max(grv_n(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grv_n(:,1)) max(grv_n(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grv_n.pdf'

%% grQ_n
for ii = 1:numel(france)
  france(ii).pop = grQ_n(ii,1); % add your data here
end

caxis([min(grQ_n(:,1)) max(grQ_n(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.2f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grQ_n(:,1)) max(grQ_n(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grQ_n.pdf'


%% grcom_n
for ii = 1:numel(france)
  france(ii).pop = grcom_n(ii,1); % add your data here
end

caxis([min(grcom_n(:,1)) max(grcom_n(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.2f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grcom_n(:,1)) max(grcom_n(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grcom_n.pdf'

%% grTcom_n
for ii = 1:numel(france)
  france(ii).pop = grTcom_n(ii,1); % add your data here
end

caxis([min(grTcom_n(:,1)) max(grTcom_n(:,1))]);
h=colorbar; %('location','southoutside');
t=get(h,'Limits');
set(h,'Ticks',linspace(t(1),t(2),3));
%set(h, 'XTickLabel')
set(h, 'XTickLabel', cellstr(num2str(reshape(get(h, 'XTick'),[],1),'%16.2f')) )
%h.TickLabels = {'First decile','Median','Tenth decile'}; 
spec = makesymbolspec('Polygon', ...
   {'pop', [min(grTcom_n(:,1)) max(grTcom_n(:,1))], 'Facecolor', parula(100)});
geoshow(france, 'SymbolSpec', spec);
export_fig '...\replication\outputs\grTcom_n.pdf'